using Senparc.Weixin.MP.Helpers;
using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace Senparc.Weixin.MP.TenPayLib
{
	public class RequestHandler
	{
		private string Key;
		protected HttpContext HttpContext;
		protected Hashtable Parameters;
		private string DebugInfo;
		public RequestHandler(HttpContext httpContext)
		{
			this.Parameters = new Hashtable();
			this.HttpContext = (httpContext ?? HttpContext.Current);
		}
		public virtual void Init()
		{
		}
		public string GetDebugInfo()
		{
			return this.DebugInfo;
		}
		public string GetKey()
		{
			return this.Key;
		}
		public void SetKey(string key)
		{
			this.Key = key;
		}
		public void SetParameter(string parameter, string parameterValue)
		{
			if (parameter != null && parameter != "")
			{
				if (this.Parameters.Contains(parameter))
				{
					this.Parameters.Remove(parameter);
				}
				this.Parameters.Add(parameter, parameterValue);
			}
		}
		public string GetRequestURL()
		{
			this.CreateSign();
			StringBuilder stringBuilder = new StringBuilder();
			ArrayList arrayList = new ArrayList(this.Parameters.Keys);
			arrayList.Sort();
			foreach (string text in arrayList)
			{
				string text2 = (string)this.Parameters[text];
				if (text2 != null && "key".CompareTo(text) != 0)
				{
					stringBuilder.Append(text + "=" + TenPayUtil.UrlEncode(text2, this.GetCharset()) + "&");
				}
			}
			if (stringBuilder.Length > 0)
			{
				stringBuilder.Remove(stringBuilder.Length - 1, 1);
			}
			return stringBuilder.ToString();
		}
		protected virtual void CreateSign()
		{
			StringBuilder stringBuilder = new StringBuilder();
			ArrayList arrayList = new ArrayList(this.Parameters.Keys);
			arrayList.Sort();
			foreach (string text in arrayList)
			{
				string text2 = (string)this.Parameters[text];
				if (text2 != null && "".CompareTo(text2) != 0 && "sign".CompareTo(text) != 0 && "key".CompareTo(text) != 0)
				{
					stringBuilder.Append(text + "=" + text2 + "&");
				}
			}
			stringBuilder.Append("key=" + this.GetKey());
			string text3 = MD5UtilHelper.GetMD5(stringBuilder.ToString(), this.GetCharset()).ToUpper();
			this.SetParameter("sign", text3);
			this.SetDebugInfo(stringBuilder.ToString() + " => sign:" + text3);
		}
		public virtual string CreateMd5Sign()
		{
			StringBuilder stringBuilder = new StringBuilder();
			ArrayList arrayList = new ArrayList(this.Parameters.Keys);
			arrayList.Sort();
			foreach (string text in arrayList)
			{
				string text2 = (string)this.Parameters[text];
				if (text2 != null && "".CompareTo(text2) != 0 && "sign".CompareTo(text) != 0 && "".CompareTo(text2) != 0)
				{
					stringBuilder.Append(text + "=" + text2 + "&");
				}
			}
			string text3 = MD5UtilHelper.GetMD5(stringBuilder.ToString(), this.GetCharset()).ToLower();
			this.SetParameter("sign", text3);
			return text3;
		}
		public string CreateSHA1Sign()
		{
			StringBuilder stringBuilder = new StringBuilder();
			ArrayList arrayList = new ArrayList(this.Parameters.Keys);
			arrayList.Sort();
			foreach (string text in arrayList)
			{
				string text2 = (string)this.Parameters[text];
				if (text2 != null && "".CompareTo(text2) != 0 && "sign".CompareTo(text) != 0 && "key".CompareTo(text) != 0)
				{
					if (stringBuilder.Length == 0)
					{
						stringBuilder.Append(text + "=" + text2);
					}
					else
					{
						stringBuilder.Append("&" + text + "=" + text2);
					}
				}
			}
			string text3 = SHA1UtilHelper.GetSha1(stringBuilder.ToString()).ToString().ToLower();
			this.SetDebugInfo(stringBuilder.ToString() + " => sign:" + text3);
			return text3;
		}
		public string ParseXML()
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("<xml>");
			foreach (string text in this.Parameters.Keys)
			{
				string text2 = (string)this.Parameters[text];
				if (Regex.IsMatch(text2, "^[0-9.]$"))
				{
					stringBuilder.Append(string.Concat(new string[]
					{
						"<",
						text,
						">",
						text2,
						"</",
						text,
						">"
					}));
				}
				else
				{
					stringBuilder.Append(string.Concat(new string[]
					{
						"<",
						text,
						"><![CDATA[",
						text2,
						"]]></",
						text,
						">"
					}));
				}
			}
			stringBuilder.Append("</xml>");
			return stringBuilder.ToString();
		}
		public void SetDebugInfo(string debugInfo)
		{
			this.DebugInfo = debugInfo;
		}
		public Hashtable GetAllParameters()
		{
			return this.Parameters;
		}
		protected virtual string GetCharset()
		{
			return this.HttpContext.Request.ContentEncoding.BodyName;
		}
	}
}
